package cc.comac.mom.datahandlers.entity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cc.comac.mom.bean.EquipPersonnelQualificationManagementOtherInterferBean;
import cc.comac.mom.entity.EquipDevEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipPersonnelQualificationManagement;
import cc.comac.mom.bean.EquipPersonnelQualificationManagementBean;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.EquipPersonnelQualificationManagementDao;
import cc.comac.mom.entity.EquipPersonnelQualificationManagementEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
@Component
@Transactional
public class EquipPersonnelQualificationManagementDataHandler extends ServiceImpl<cc.comac.mom.dao.entity.EquipPersonnelQualificationManagementDao, EquipPersonnelQualificationManagementEntity>{

	@Autowired
	private EquipPersonnelQualificationManagementDao equipPersonnelQualificationManagementDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean insertEquipPersonnelQualificationManagement (EquipPersonnelQualificationManagement equipPersonnelQualificationManagement) {
		ResultBean resultBean = new ResultBean(true);
		EquipPersonnelQualificationManagementEntity innerData = equipPersonnelQualificationManagement.getInnerData();
		if (innerData.getEquipDevId() == 0) {
			throw new RuntimeException("新增无法获取设备编号！");
		}
		if (StringUtils.isEmpty(innerData.getJobNumber())) {
			throw new RuntimeException("新增人员工号不能为空！");
		}
		if (StringUtils.isEmpty(innerData.getPersonName())) {
			throw new RuntimeException("新增人员姓名不能为空！");
		}
		// 校验是否存在新增数据
		QueryWrapper<EquipPersonnelQualificationManagementEntity> queryWrapper = new QueryWrapper<EquipPersonnelQualificationManagementEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("job_number", innerData.getJobNumber());
		queryWrapper.eq("equip_dev_id", innerData.getEquipDevId());
		EquipPersonnelQualificationManagementEntity selectOne = equipPersonnelQualificationManagementDao.selectOne(queryWrapper);
		if (selectOne != null) {
			throw new RuntimeException("工号"+selectOne.getJobNumber()+"对应的部门"+selectOne.getDepartment()+"已存在！");
		}
		// 保存新增数据
		baseEntityDataUtil.setBaseEntityData(innerData);
		equipPersonnelQualificationManagementDao.insert(innerData);
		return resultBean;
	}
	
	public ResultBean updateEquipPersonnelQualificationManagement (EquipPersonnelQualificationManagement equipPersonnelQualificationManagement) {
		ResultBean resultBean = new ResultBean(true);
		EquipPersonnelQualificationManagementEntity innerData = equipPersonnelQualificationManagement.getInnerData();
		if (innerData.getId() == 0) {
			throw new RuntimeException("无法获取编辑数据主键");
		}
		if (innerData.getEquipDevId() == 0) {
			throw new RuntimeException("新增无法获取设备编号！");
		}
		if (StringUtils.isEmpty(innerData.getPersonName())) {
			throw new RuntimeException("新增人员姓名不能为空！");
		}
		if (innerData.getEquipDevId() == 0) {
			throw new RuntimeException("新增设备信息不能为空！");
		}
		// 判断是否存在对应数据
		QueryWrapper<EquipPersonnelQualificationManagementEntity> queryWrapper = new QueryWrapper<EquipPersonnelQualificationManagementEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("job_number", innerData.getJobNumber());
		queryWrapper.eq("equip_dev_id", innerData.getEquipDevId());
		queryWrapper.ne("personnel_qualification_id", innerData.getId());
		EquipPersonnelQualificationManagementEntity selectOne = equipPersonnelQualificationManagementDao.selectOne(queryWrapper);
		if (selectOne != null) {
			throw new RuntimeException("工号"+selectOne.getJobNumber()+"对应的部门"+selectOne.getDepartment()+"已存在！");
		}
		equipPersonnelQualificationManagementDao.updateById(innerData);
	return resultBean;
	}
	
	public ResultBean batchDelete (List<EquipPersonnelQualificationManagement> list) {
		ResultBean resultBean = new ResultBean(true);
		// 预留删除校验
		// ......
		
		List<EquipPersonnelQualificationManagementEntity> entityList = new ArrayList<>();
		for (EquipPersonnelQualificationManagement equipPersonnelQualificationManagement : list) {
			EquipPersonnelQualificationManagementEntity innerData = equipPersonnelQualificationManagement.getInnerData();
			EquipPersonnelQualificationManagementEntity equipPersonnelQualificationManagementEntity = new EquipPersonnelQualificationManagementEntity();
			equipPersonnelQualificationManagementEntity.setId(innerData.getId());
			equipPersonnelQualificationManagementEntity.setRowStatus(0);
			baseEntityDataUtil.updateBaseEntityData(equipPersonnelQualificationManagementEntity);
			entityList.add(equipPersonnelQualificationManagementEntity);
		}
		
		updateBatchById(entityList, entityList.size());
		return resultBean;
	}
	
	// 通过条件查询对应数据
	public ResultBean selectbySomeFields (EquipPersonnelQualificationManagementBean equipPersonnelQualificationManagementBean) {
		ResultBean resultBean = new ResultBean(true);
		// 校验是否存在分页数据
		LimitPageBean<EquipPersonnelQualificationManagementBean> limitPageBean = new LimitPageBean<EquipPersonnelQualificationManagementBean>();
		if (equipPersonnelQualificationManagementBean.getPageNum() == 0) {
			equipPersonnelQualificationManagementBean.setPageNum(1);
		}
		if (equipPersonnelQualificationManagementBean.getPageSize() == 0) {
			equipPersonnelQualificationManagementBean.setPageSize(10);
		}
		int pageNum = equipPersonnelQualificationManagementBean.getPageNum();
		equipPersonnelQualificationManagementBean.setPageNum((equipPersonnelQualificationManagementBean.getPageNum()-1)*equipPersonnelQualificationManagementBean.getPageSize());
		// 通过条件查询数据集合
		List<EquipPersonnelQualificationManagementBean> selectBysomeFeilds = equipPersonnelQualificationManagementDao.selectBySomeFields(equipPersonnelQualificationManagementBean);
		// 通过条件查询数据总数
		int selectCountBysomeFields = equipPersonnelQualificationManagementDao.selectCountBysomeFields(equipPersonnelQualificationManagementBean);
		int ceil = (int)Math.ceil(selectCountBysomeFields * 1.0d / equipPersonnelQualificationManagementBean.getPageSize());
		limitPageBean.setEntityList(selectBysomeFeilds);
		limitPageBean.setPageCount(ceil);
		limitPageBean.setPageSize(equipPersonnelQualificationManagementBean.getPageSize());
		limitPageBean.setPageNum(pageNum);
		limitPageBean.setTotalQuantity(selectCountBysomeFields);
		resultBean.setObject(limitPageBean);
		return resultBean;
	}
	
	// 查询所有数据集合
	public ResultBean selectAllData () {
		ResultBean resultBean = new ResultBean(true);
		LimitPageBean<EquipPersonnelQualificationManagementBean> limitPageBean = new LimitPageBean<EquipPersonnelQualificationManagementBean>();
		QueryWrapper<EquipPersonnelQualificationManagementEntity> queryWrapper = new QueryWrapper<EquipPersonnelQualificationManagementEntity>();
		queryWrapper.eq("row_status", 1);
		List<EquipPersonnelQualificationManagementEntity> selectList = equipPersonnelQualificationManagementDao.selectList(queryWrapper);
		limitPageBean.setEntityList(selectList);
		resultBean.setObject(limitPageBean);
		return resultBean;
	}
	public ResultBean batchsave (List<EquipPersonnelQualificationManagementOtherInterferBean> personnel) {
		ResultBean resultBean = new ResultBean(true);
		// 查询那些是修改的，哪些是新增的
		List<EquipPersonnelQualificationManagementEntity> hisList=equipPersonnelQualificationManagementDao.selectPersonnelInfoByPersonName(personnel);
		//把重复的数据放入map中
		Map<String,EquipPersonnelQualificationManagementEntity> hisMap=new HashMap<>();
		if(hisList!=null&&hisList.size()>0){
			for(EquipPersonnelQualificationManagementEntity e:hisList){
				hisMap.put(e.getJobNumber()+"_"+e.getDepartment(),e);
			}
		}
		//根据设备编号查询设备ID
		List<EquipDevEntity> equipList=equipPersonnelQualificationManagementDao.selectDevIdByDevNumber(personnel);
		Map<String,EquipDevEntity> equipMap=new HashMap<>();
		if(equipList!=null&&equipList.size()>0){
			for(EquipDevEntity e:equipList){
				equipMap.put(e.getDevNumber(),e);
			}
		}
		List<EquipPersonnelQualificationManagementEntity> insertList=new ArrayList<>();
		List<EquipPersonnelQualificationManagementEntity> updateList=new ArrayList<>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(EquipPersonnelQualificationManagementOtherInterferBean e:personnel){
			EquipPersonnelQualificationManagementEntity temp=new EquipPersonnelQualificationManagementEntity();
			temp.setDepartment(e.getDepartment());
			try {
				temp.setEffectiveTimeEnd(sdf.parse(e.getEffectiveTimeEnd()));
				temp.setEffectiveTimeStart(sdf.parse(e.getEffectiveTimeStart()));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			temp.setJobNumber(e.getJobNumber());
			temp.setPersonName(e.getPersonName());
			temp.setQualificationsStatus(e.getQualificationsStatus());
			if(equipMap.containsKey(e.getDevNumber())) {
				temp.setEquipDevId(equipMap.get(e.getDevNumber()).getId());
			}
			if(!hisMap.containsKey(e.getJobNumber()+"_"+e.getDepartment())){
				baseEntityDataUtil.setBaseEntityData(temp);
				insertList.add(temp);
			}else{
				baseEntityDataUtil.updateBaseEntityData(temp);
				temp.setId(hisMap.get(e.getJobNumber()+"_"+e.getDepartment()).getId());
				updateList.add(temp);
			}

		}
		//保存人员资质
		if(insertList!=null&&insertList.size()>0){
			equipPersonnelQualificationManagementDao.insertBatchPersonnelInfo(insertList);
		}
		//修改人员资质
		if(updateList!=null&&updateList.size()>0){
			equipPersonnelQualificationManagementDao.updateBatchPersonnelInfo(updateList);
		}
		return resultBean;
	}
}
